from django import forms
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError
from django.shortcuts import render
from . import models


# Create your views here.
class RegisterModelForm(forms.ModelForm):
    mobile_phone = forms.CharField(label="手机号码",
                                   validators=[RegexValidator(r"^(1[3|4|5|6|7|8|9])\d{9}$", "手机号码格式错误"), ])
    password = forms.CharField(label="密码", widget=forms.PasswordInput())
    confirm_password = forms.CharField(label="确认密码", widget=forms.PasswordInput())
    code = forms.CharField(label="验证码")

    class Meta:
        model = models.UserInfo
        # fields = '__all__'
        fields = ['email', 'username', 'password', 'confirm_password', 'mobile_phone', 'code']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # 设置字段排序
        # ordering = ['email', 'username', 'password', 'confirm_password', 'mobile_phone', 'code']
        # self.order_fields(ordering)

        # 设置每个字段的样式和文本框的默认值
        for name, field in self.fields.items():
            field.widget.attrs['class'] = 'form-control'
            field.widget.attrs['placeholder'] = f"请输入{field.label}"


def register(request):
    form = RegisterModelForm()
    return render(request, "my_tracer/register.html", {'form': form})
